package com.itheima.a04xpathdemo;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;

public class XPathDemo1 {
    public static void main(String[] args) throws DocumentException {
        //1.创建一个解析器对象
        SAXReader saxReader = new SAXReader();
        //2.解析xml文件，并且返回文档对象
        Document document = saxReader.read(new File("sockethomework\\src\\com\\itheima\\a04xpathdemo\\person.xml"));
        //3.利用XPath方式来进行快速检索
        //两个方法
        //1.检索单个   document.selectSingleNode("路径")
        //2.检索多个   document.selectNodes("路径")

        //检索多个
        /*List<Element> list = document.selectNodes("/persons/person/name");
        for (Element element : list) {
            System.out.println(element.getText());
        }*/

        //检索单个（标签相同检索第一个）
        Element element = (Element) document.selectSingleNode("/persons/person/name");
        System.out.println(element.getText());
    }
}
